home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 2324 / 2324.xpi / chrome / sessionmanager.jar / content / sessionmanager / options.js < prev    next >
Encoding:
JavaScript  |  2009-10-08  |  14.2 KB  |  393 lines

  1. var originalOverwriteLabel = null;
  2.  
  3. gSessionManager._onLoad = gSessionManager.onLoad;
  4. gSessionManager.onLoad = function() {
  5.     this._onLoad(true);
  6.     
  7.     // If instant Apply is on, hide the apply button
  8.     if (this.getPref("browser.preferences.instantApply", false, true)) {
  9.         _("sessionmanagerOptions").getButton("extra1").style.visibility = "collapse";
  10.     }
  11.     
  12.     // Populate select session list and select previously selected session
  13.     var resume_session = _("resume_session");
  14.     var sessions = this.getSessions();
  15.     resume_session.appendItem(this._string("startup_resume"), this.mBackupSessionName, "");
  16.     var maxWidth = window.getComputedStyle(_("startEndGroupbox"), null).width;
  17.     sessions.forEach(function(aSession) {
  18.         if ((aSession.fileName != this.mAutoSaveSessionName) && (aSession.fileName != this.mBackupSessionName))
  19.         {
  20.             var elem = resume_session.appendItem(aSession.name, aSession.fileName, "");
  21.             elem.setAttribute("maxwidth", maxWidth);
  22.             elem.setAttribute("crop", "center");
  23.         }
  24.     }, this);
  25.     // if no restore value, select previous browser session
  26.     resume_session.value = _("extensions.sessionmanager.resume_session").value || this.mBackupSessionName;
  27.     
  28.     // current load session no longer there
  29.     if (resume_session.selectedIndex == -1) {
  30.         resume_session.value ="";
  31.         _("extensions.sessionmanager.resume_session").valueFromPreferences = resume_session.value;
  32.         // change option to none if select session was selected
  33.         if (_("startupOption").selectedIndex==2) {
  34.             _("startupOption").selectedIndex = 0;
  35.             _("extensions.sessionmanager.startup").valueFromPreferences = _("startupOption").selectedIndex;
  36.         }
  37.     }
  38.     
  39.     // Restore selected indexes and hide/show menus for startup options
  40.     _("generalPrefsTab").selectedIndex = _("extensions.sessionmanager.options_selected_tab").valueFromPreferences;
  41.     startupSelect(_("startupOption").selectedIndex = _("extensions.sessionmanager.startup").valueFromPreferences);
  42.     
  43.     // Hide close tab restoration preferences in SeaMonkey since it doesn't work
  44.     if (this.mApplication.name.toUpperCase() == "SEAMONKEY") {
  45.         _("save_closed_tabs").parentNode.style.visibility = "collapse";
  46.     }
  47.     
  48.     // Hide option to use built in SessionStore closed window list if not supported
  49.     if (typeof(this.mSessionStore.getClosedWindowCount) != "function") {
  50.         _("closed_window_list").style.visibility = "collapse";
  51.     }
  52.     checkClosedWindowList(_("extensions.sessionmanager.use_SS_closed_window_list").valueFromPreferences);
  53.     
  54.     // Change overwrite label to tabs if append to window as tab preference set
  55.     originalOverwriteLabel = _("overwrite").label;
  56.     changeOverwriteLabel(_("extensions.sessionmanager.append_by_default").valueFromPreferences);
  57.     
  58.     // Disable or enable "allow saving in PBM" depending on encryption state
  59.     _("allow_save_in_pbm").disabled = !_("encrypt_sessions").checked;
  60.     
  61.     // Hide mid-click preference if Tab Mix Plus or Tab Clicking Options is enabled
  62.     var browser = this.mWindowMediator.getMostRecentWindow("navigator:browser");
  63.     if (browser) {
  64.         if ((typeof(browser.tabClicking) != "undefined") || (typeof(browser.TM_checkClick) != "undefined")) {
  65.             _("midClickPref").style.visibility = "collapse";
  66.         }
  67.         
  68.         if (browser.gSingleWindowMode) {
  69.             _("overwrite").label = gSessionManager._string("overwrite_tabs");
  70.             _("open_as_tabs").style.visibility = "collapse";
  71.         }
  72.     }
  73.     
  74.     // Update Logging Level checkboxes
  75.     readLogLevel();
  76.  
  77.     // Disable Apply Button by default
  78.     _("sessionmanagerOptions").getButton("extra1").disabled = true;
  79.  
  80.     adjustContentHeight();
  81. };
  82.  
  83. gSessionManager.onWindowClose = function() {};
  84.  
  85. gSessionManager.onUnload = function() {
  86.     _("extensions.sessionmanager.options_selected_tab").valueFromPreferences = _("generalPrefsTab").selectedIndex;
  87.     setLogLevel();
  88. };
  89.  
  90. var _disable = gSessionManager.setDisabled;
  91.  
  92. function readMaxClosedUndo(aID)
  93. {
  94.     switch (aID) {
  95.         case "max_closed":
  96.             var value = _("extensions.sessionmanager.max_closed_undo").value;
  97.             _disable(_("save_window_list"), value == 0);
  98.             return value;
  99.             break;
  100.         case "max_closed_SS":
  101.             var value = _("browser.sessionstore.max_windows_undo").value;
  102.             _disable(_("save_closed_windows"), value == 0);
  103.             _disable(document.getElementsByAttribute("control", "save_closed_windows")[0], value == 0);
  104.             return value;
  105.             break;
  106.     }
  107.     
  108.     return 0;
  109. }
  110.  
  111. function readMaxTabsUndo()
  112. {
  113.     var value = _("browser.sessionstore.max_tabs_undo").value;
  114.     
  115.     _disable(_("save_closed_tabs"), value == 0);
  116.     _disable(document.getElementsByAttribute("control", "save_closed_tabs")[0], value == 0);
  117.     
  118.     return value;
  119. }
  120.  
  121. function promptClearUndoList()
  122. {
  123.     var max_tabs_undo = _("max_tabs").value;
  124.     
  125.     gSessionManager.clearUndoListPrompt();
  126.     
  127.     _("max_tabs").value = max_tabs_undo;
  128. };
  129.  
  130. function readInterval()
  131. {
  132.     return _("browser.sessionstore.interval").value / 1000;
  133. }
  134.  
  135. function writeInterval()
  136. {
  137.     return Math.round(parseFloat(_("interval").value) * 1000 || 0);
  138. }
  139.  
  140. function readPrivacyLevel()
  141. {
  142.     var value = _("browser.sessionstore.privacy_level").value;
  143.     
  144.     _disable(_("postdata"), value > 1);
  145.     _disable(document.getElementsByAttribute("control", "postdata")[0], value > 1);
  146.     
  147.     return value;
  148. }
  149.  
  150. function logLevelUpdate() {
  151.     // If instant apply on, apply immediately
  152.     if (gSessionManager.getPref("browser.preferences.instantApply", false, true)) {
  153.         setLogLevel();
  154.     }
  155.     else enableApply();
  156. }
  157.  
  158. function setLogLevel() {
  159.     var logLevel = 0;
  160.     var logCB = document.getElementsByAttribute("class", "logLevel");
  161.     for (var i=0; i < logCB.length; i++) {
  162.         logLevel = logLevel | (logCB[i].checked ? gSessionManager.logging_level[logCB[i].getAttribute("_logLevel")] : 0);
  163.     };
  164.     
  165.     _("extensions.sessionmanager.logging_level").valueFromPreferences = logLevel;
  166. }
  167.  
  168. function readLogLevel() {
  169.     var logLevel = _("extensions.sessionmanager.logging_level").valueFromPreferences;
  170.     var logCB = document.getElementsByAttribute("class", "logLevel");
  171.     for (var i=0; i < logCB.length; i++) {
  172.         logCB[i].checked = ((logLevel & gSessionManager.logging_level[logCB[i].getAttribute("_logLevel")]) > 0);
  173.     };
  174. }
  175.  
  176. function _(aId)
  177. {
  178.     return document.getElementById(aId);
  179. }
  180.  
  181. function selectSessionDir() {
  182.     var nsIFilePicker = Components.interfaces.nsIFilePicker;
  183.     var filepicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  184.  
  185.     filepicker.init(window, gSessionManager._string("choose_dir"), nsIFilePicker.modeGetFolder);
  186.     filepicker.appendFilters(nsIFilePicker.filterAll);
  187.     var ret = filepicker.show();
  188.     if (ret == nsIFilePicker.returnOK) {
  189.         _("extensions.sessionmanager.sessions_dir").value = filepicker.file.path;
  190.     }
  191. }      
  192.  
  193. function defaultSessionDir() {
  194.     _("extensions.sessionmanager.sessions_dir").value = '';
  195. }
  196.  
  197. function checkEncryption(aState) {
  198.     try {
  199.         // force a master password prompt so we don't waste time if user cancels it
  200.         gSessionManager.mSecretDecoderRing.encryptString("");
  201.     }
  202.     catch (ex) {
  203.         gSessionManager.cryptError(gSessionManager._string("change_encryption_fail"));
  204.         return !aState;
  205.     }
  206.     _("encrypted_only").hidden = !aState;
  207.     _("allow_save_in_pbm").disabled = !aState;
  208.     
  209.     // When animating preferences the window can get cut off so just refresh the window size here
  210.     if (aState && gSessionManager.getPref("browser.preferences.animateFadeIn", false, true))
  211.         window.sizeToContent();
  212.     
  213.     return aState;
  214. }
  215.  
  216. function checkEncryptOnly(aState) {
  217.     if (aState && !_("extensions.sessionmanager.encrypted_only").valueFromPreferences) {
  218.         if (!gSessionManager.mPromptService.confirm(window, gSessionManager.mTitle, gSessionManager._string("encrypt_only_confirm"))) {
  219.             aState = false;
  220.         }
  221.     }
  222.     
  223.     return aState;
  224. }
  225.  
  226. function changeOverwriteLabel(aChecked) {
  227.     _("overwrite").label = aChecked ? gSessionManager._string("overwrite_tabs") : originalOverwriteLabel;
  228. }
  229.  
  230. function checkClosedWindowList(aChecked) {
  231.     // Hide the option to not clear the list of closed windows on shutdown if we are using the built in closed windows
  232.     var builtin = aChecked && (_("closed_window_list").style.visibility != "collapse");
  233.     
  234.     _("save_window_list").style.visibility = builtin ? "collapse" : "visible";
  235.     _("max_closed").style.visibility = builtin ? "collapse" : "visible";
  236.     _("max_closed_SS").style.visibility = builtin ? "visible" : "collapse";
  237.     _("closed_windows_menu").style.visibility = builtin ? "visible" : "collapse";
  238. }
  239.  
  240. function startupSelect(index) {
  241.     // hide/display corresponding menus    
  242.     _("browserStartupPage").style.visibility = (index != 0)?"collapse":"visible";
  243.     _("preselect").style.visibility = (index != 1)?"collapse":"visible";
  244.     _("resume_session").style.visibility = (index != 2)?"collapse":"visible";
  245.     //if (index == 1) _("resume_session").style.visibility = "hidden";
  246.     
  247.     // If instant apply on, apply immediately
  248.     if (gSessionManager.getPref("browser.preferences.instantApply", false, true)) {
  249.         setStartValue();
  250.     }
  251. }
  252.  
  253. function setStartValue() {
  254.     _("extensions.sessionmanager.startup").valueFromPreferences = _("startupOption").selectedIndex;
  255. }
  256.  
  257. function savePrefs() {
  258.     var prefs = document.getElementsByTagName('preference');
  259.     for (var i=0; i<prefs.length; i++) {
  260.         prefs[i].valueFromPreferences = prefs[i].value;
  261.     }
  262.     setStartValue();
  263.     
  264.     // Disable Apply Button
  265.     _("sessionmanagerOptions").getButton("extra1").disabled = true;
  266. }    
  267.  
  268. function enableApply() {
  269.     _("sessionmanagerOptions").getButton("extra1").disabled = false;
  270. }
  271.  
  272. function goHelp() {
  273.     var link = "http://sessionmanager.mozdev.org/options.html#";
  274.     
  275.     switch (_("sessionmanagerOptions").currentPane) {
  276.         case (_("mainPrefPane")):
  277.             switch (_("generalPrefsTab").selectedIndex) {
  278.                 case 0:
  279.                     link = link + "startup";
  280.                     break;
  281.                 case 1:
  282.                     link = link + "saving";
  283.                     break;
  284.                 case 2:
  285.                     link = link + "display";
  286.                     break;
  287.             }
  288.             break;
  289.         case (_("undoclosePrefPane")):
  290.             link = link + "undo";
  291.             break;
  292.         case (_("advancedPrefPane")):
  293.             link = link + "advanced";
  294.             break;
  295.         case (_("sessionstorePrefPane")):
  296.             link = link + "sessionstore";
  297.             break;
  298.         case (_("loggingPrefPane")):
  299.             link = link + "logging";
  300.             break;
  301.     }
  302.     
  303.     openLink(link);
  304. }
  305.  
  306. function openLink(url) {
  307.     var top = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  308.              .getService(Components.interfaces.nsIWindowMediator).getMostRecentWindow("navigator:browser");
  309.                      
  310.     if (!top) window.open(url, "", "");
  311.     else {
  312.         var tBrowser = top.getBrowser();
  313.         var currBlank = false;
  314.             
  315.         // Is current tab blank or already on help page.
  316.         if (tBrowser && tBrowser.mCurrentTab.linkedBrowser) {
  317.             var location = tBrowser.mCurrentTab.linkedBrowser.contentDocument.location.href;
  318.             var index = location.indexOf("#");
  319.             var baseLocation = (index == -1)? location : location.substring(0,index);
  320.             index = url.indexOf("#");
  321.             var baseURL = (index == -1)? url : url.substring(0,index);
  322.             currBlank = (location == "about:blank") || (baseLocation == baseURL);
  323.         }
  324.                                    
  325.         if (currBlank) tBrowser.loadURI(url);
  326.         else {
  327.             var tab = tBrowser.addTab(url);
  328.             tBrowser.selectedTab = tab;
  329.         }
  330.     }
  331. }
  332.  
  333. function adjustContentHeight() {
  334.     // Localize strings aren't used when the initial height is used to calculate the size of the context-box
  335.     // and preference window.  The height is calculated correctly once the window is drawn, but the context-box
  336.     // and preference window heights are never updated.
  337.     // To fix this, we need to explicitly set the height style of any element with a localized string that is more 
  338.     // than one line (the descriptions).  This will correct the heights when the panes are selected.
  339.     var largestNewPaneHeight = 0;
  340.     var largestCurrentPaneHeight = 0;
  341.     var biggestPane = null;
  342.     for (var i=0; i < _("sessionmanagerOptions").preferencePanes.length; i++) {
  343.         var pane = _("sessionmanagerOptions").preferencePanes[i];
  344.         var descriptions = pane.getElementsByTagName('description');
  345.         var adjustHeight = 0;
  346.         for (var j=0; j<descriptions.length; j++) {
  347.             var height = window.getComputedStyle(descriptions[j], null).height;
  348.             if (height != "auto") {
  349.                 descriptions[j].style.height = height;
  350.                 adjustHeight += parseInt(height) - 26;
  351.             }
  352.         }
  353.         adjustHeight = pane.contentHeight + adjustHeight;
  354.         if (adjustHeight > largestNewPaneHeight) {
  355.             largestNewPaneHeight = adjustHeight;
  356.             biggestPane = pane;
  357.         }
  358.         if (pane.contentHeight > largestCurrentPaneHeight) 
  359.             largestCurrentPaneHeight = pane.contentHeight;
  360.     }
  361.     // The exception to this is if the largest pane is already selected when the preference window is opened.  In
  362.     // this case the window inner height must be correct as well as the context-box height (if animation is disabled).
  363.     var currentPane = _("sessionmanagerOptions").currentPane;
  364.     var animate = gSessionManager.getPref("browser.preferences.animateFadeIn", false, true);
  365.  
  366.     // When not animating, the largest pane's content height is not correct when it is opened first so update it.
  367.     // Also the window needs to be resized to take into account the changes to the description height.
  368.     if (!animate) {
  369.         // For some reason if opening the largest (Advanced) pane first and the encrypt only box is checked, the size is wrong so tweak it.
  370.         if ((currentPane == biggestPane) && !_("encrypted_only").hidden) largestNewPaneHeight += 12;
  371.         biggestPane._content.height = largestNewPaneHeight;
  372.         window.sizeToContent();
  373.     }
  374.     // When animating the window needs to be resized to take into account the changes to the description height and
  375.     // then shrunk since the opening pane is sized to the largest pane height which is wrong.
  376.     else {
  377.         // Hide/show the encrypt only check box here when opening the largest pane to prevent window looking to large.
  378.         if (currentPane == biggestPane) {
  379.             _("encrypted_only").hidden = !_("encrypt_sessions").checked;
  380.         }
  381.     
  382.         window.sizeToContent();
  383.         // If encrypted only checkbox is hidden need to tweak the height
  384.         var adjuster = (_("encrypted_only").hidden) ? (2 * largestCurrentPaneHeight - largestNewPaneHeight) : largestCurrentPaneHeight;
  385.         window.innerHeight -= adjuster - currentPane.contentHeight;
  386.     }
  387.     
  388.     // Hide/show the encrypt only checkbox based on state of encryption checkbox
  389.     _("encrypted_only").hidden = !_("encrypt_sessions").checked;
  390.     
  391.     // Re-select same pane to refresh it - General pane's tab boxes aren't the right height unless the General tab is re-selected
  392.     _("sessionmanagerOptions")._selector.selectedItem.click();
  393. }